// 用户状态信息管理
import { defineStore } from "pinia";
import type { UserInfo } from "@/types/UserInfo";
import { isTeacher } from '@/api/login/userLogin';
import { getUserMeta } from '@/api/PersonalSpace/PersonalInfo';
import { postUserMicroScenePermission } from '@/api/MicroScene/MicroScenePermission';

interface UserInfoState {
  userInfo: UserInfo;
  isTeacher: boolean;
  // 是否具有编辑微场景权限
  isEditMicroScene: boolean;
  // 是否具有查看微场景权限
  isViewMicroScene: boolean;
  // 是否具有创建微场景权限
  isCreateMicroScene: boolean;
}

export const useUserStore = defineStore("userStore", {
  // 设置默认值
  state: (): UserInfoState => ({
    userInfo: {
      nickName: "",
      sex: "",
      userIntroduction: "",
      userImage: "",
      userCover: "",
      realName: "",
      school: "",
      college: "",
      major: "",
      studentId: "",
      phoneNumber: "",
      email: "",
      participationType: "",
      campus: "",
      studentStatus: "",
      enrollmentYear: "",
      projectStatus: "",
      projectType: "",
      projectName: "",
    },
    isTeacher: false,
    isEditMicroScene: false,
    isViewMicroScene: false,
    isCreateMicroScene: false,
  }),
  actions: {
    async setUserInfo() {
      this.userInfo = await getUserMeta();
    },
    async setIsTeacher() {
      this.isTeacher = await isTeacher();
    },
    async setIsMicroScenePermission(microSceneId: string) {
      const permission = await postUserMicroScenePermission(microSceneId);
      this.isEditMicroScene = permission.edit;
      this.isViewMicroScene = permission.view;
      this.isCreateMicroScene = permission.create;
    },
  },
  persist: {
    key: 'user-store',
    storage: localStorage,
  },
});
